/**
 * (C) Copyright MarketLive. 2007. All rights reserved.
 * MarketLive is a trademark of MarketLive, Inc.
 * Warning: This computer program is protected by copyright law and international treaties.
 * Unauthorized reproduction or distribution of this program, or any portion of it, may result
 * in severe civil and criminal penalties, and will be prosecuted to the maximum extent
 * possible under the law.
 */
package com.marketlive.system.keymanagement;

/**
 * Class attempts to erase characters echoed to the console.
 */
public class MaskingThread extends Thread {
    
    private volatile boolean stop;
    private char echochar = '*';

   /**
    *@param prompt The prompt displayed to the user
    */
    public MaskingThread(String prompt) {
       System.out.print(prompt);
    }

   /**
    * Begin masking until asked to stop.
    */
    public void run() {

       int priority = Thread.currentThread().getPriority();
       Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

       try {
          stop = true;
          while(stop) {
            System.out.print("\010" + echochar);
            try {
               // attempt masking at this rate
               Thread.sleep(1);
            }catch (InterruptedException iex) {
               Thread.currentThread().interrupt();
               return;
            }
          }
       } finally { // restore the original priority
          Thread.currentThread().setPriority(priority);
       }
    }

   /**
    * Instruct the thread to stop masking.
    */
    public void stopMasking() {
       this.stop = false;
    }
}
